//点击添加按钮 跳转到添加地址的页面
$(".add-btn").click(function () {
    window.location.href ="/fresh/address/add.jsp";
})




//查看用户所有的地址信息
getAll();
function getAll() {
    var customerId =  $(".customerid").val();
    console.log(customerId)
    $.ajax({
        type:"GET",
        url:"/fresh/address/getAll/"+customerId,
        dataType:"json",
        success:function (result) {
            /*console.log("result"+result);*/
            var addressList = result.data;
            if (result.status==1){
                //页面地址信息渲染


                $(".all-address-ul").html("");
                var str = "";
                $.each(addressList,function (index,address) {
                    var addressId = address.addressId;
                    var addressName = address.addressName;
                    var addressCust = address.addressCust;
                    var addressProvince = address.addressProvince;
                    var addressCity = address.addressCity;
                    var addressArea = address.addressArea;
                    var addressDetails = address.addressDetails;
                    var addressPhone = address.addressPhone;

                    var ele="<li class=\"all-address-li\">\n" +
                        "                        <!--模仿京东的地址-->\n" +
                        "                        <div class=\"sm\">\n" +
                        "                            <input type=\"hidden\" name=\"addressId\" class=\"addressId\" value="+addressId+"><br>\n" +
                        "                            <div class=\"smt\">\n" +
                        "                                <h3>"+addressName+"</h3>\n" +
                        "                                <a href=\"javascript:deleteBtn("+addressId+");\" class=\"del-btn\">删除</a>\n" +
                        "                            </div>\n" +
                        "                            <!--收件地址信息-->\n" +
                        "                            <div class=\"item-local\">\n" +
                        "                                <div class=\"item\">\n" +
                        "                                    <span class=\"label\">收件人:</span>\n" +
                        "                                    <span class=\"f1\">"+addressCust+"</span>\n" +
                        "                                </div>\n" +
                        "\n" +
                        "                                <div class=\"item\">\n" +
                        "                                    <span class=\"label\">所在地区:</span>\n" +
                        "                                    <span class=\"f2\">"+addressProvince+addressCity+addressArea+"</span>\n" +
                        "                                </div>\n" +
                        "\n" +
                        "                                <div class=\"item\">\n" +
                        "                                    <span class=\"label\">地址:</span>\n" +
                        "                                    <span class=\"f3\">"+addressDetails+"</span>\n" +
                        "                                </div>\n" +
                        "\n" +
                        "                                <div class=\"item\">\n" +
                        "                                    <span class=\"label\">手机号码:</span>\n" +
                        "                                    <span class=\"f4\">"+addressPhone+"</span>\n" +
                        "                                </div>\n" +
                        "                                <div class=\"modify\">\n" +
                        "                                    <a href=\"javascript:;\" class=\"modify-btn\"></a>\n" +
                        "                                </div>\n" +
                        "\n" +
                        "                            </div>\n" +
                        "\n" +
                        "                        </div>\n" +
                        "\n" +
                        "                    </li>"


                    str +=ele;
                })

                   $(".all-address-ul").html(str);
                getCount();
            }else {
                console.log("未查询到数据");
            }
        }
    })

}


//删除
function deleteBtn(addressId) {

    console.log("addressId："+addressId);

    $.ajax({
        type: "GET",
        url:"/fresh/address/delete/"+addressId,
        datatype:"json",
        success:function (result) {
            console.log(result);
            if (result.status==1){
                getAll();
                getCount();
            }else {
                console.log("删除失败");
            }

        }
    })
}


//获取地址个数
getCount();
function getCount() {
    var customerId =  $(".customerid").val();
    $.ajax({
        type:"GET",
        url:"/fresh/address/getCount/"+customerId,
        dataType: "json",
        success:function (result) {
            if (result.status==1){
                console.log("查询成功");
                $(".addressNum-top").html("");
                var str = "";
                str = result.data;
                $(".addressNum-top").html(str);
            }else {
                console.log("查询失败");
            }
        }
    })
}

